package mathsTrainingSystem;
import java.util.Random;
import java.util.Scanner;
import mathsTrainingSystem.ErrorRecords.ErrorRecorder;

public class Grade_One {
	
	public static void main(String[]args) {
		//grade_One();
	}
	
	public static void modifyErrorRecorder(ErrorRecords errorRecords, int index, int a, int b, int c, int flag, int tag, int asw) {  
        ErrorRecorder recorder = errorRecords.getErrorRecorder(index);  
        if (recorder != null) {  
            recorder.a = a;
            recorder.b = b;
            recorder.c = c;
            recorder.flag = flag;
            recorder.tag = tag;
            recorder.asw = asw;
        }  
    } 
	
	public static void grade_One(ErrorRecords errorRecords) {
		System.out.println("你选择了一年级题目");
		System.out.println("\n请选择你要练习的题型：\n" + "[1]10以内加减法：1步计算            [2]10以内加减法：2步计算\n[3]10-20加减法：1步或2步计算        [4]综合训练");
		Scanner scanner = new Scanner(System.in);
		int choice = 0;
		while (choice != 1 && choice != 2 && choice != 3 && choice != 4) {
			while (!scanner.hasNextInt()) {  
	            System.out.println("输入无效，请重新输入一个整数: ");  
	            scanner.next();
	        } 
			choice = scanner.nextInt();
		}
		int number = 0;
		while (number != 1 && number != 2) {
			System.out.print("请选择题量：[1]10" + "[2]20");
			while (!scanner.hasNextInt()) {  
	            System.out.println("输入无效，请重新输入一个整数: ");  
	            scanner.next();
	        } 
			number = scanner.nextInt(); 
		}
		number *= 10;
		switch (choice) {
		case 1:
			ShiYiNeiYiCiJiaJian(number, 500, errorRecords);
			break;
			
		case 2:
			ShiDaoErShiYiCiJiaJian(number, 500, errorRecords);
			break;
			
		case 3:
			caculate_10_20(number, 500, errorRecords);
			break;
		
		case 4:
			comprehensive_Exercise(number, errorRecords);
			
		default:
			break;
		}
	}
	
	public static void comprehensive_Exercise (int number, ErrorRecords errorRecords) {
		Random rand = new Random();
		Scanner scanner = new Scanner(System.in);
		int num;
		int i = 1;
		while (i <= number) {
			num = rand.nextInt(10) + 1;
			switch(num%3) {
			case 0:
				System.out.println("第"+ i +"题");
				ShiYiNeiYiCiJiaJian(1, i, errorRecords);
				break;
			
			case 1:
				System.out.println("第"+ i +"题");
				ShiYiNeiErCiJiaJian(1, i, errorRecords);
				break;
				
			case 2:
				System.out.println("第"+ i +"题");
				caculate_10_20(1, i, errorRecords);
				break;
			
			default:
				break;
			}
			i++;
		}
	}
	
	public static void ShiYiNeiYiCiJiaJian(int number, int index, ErrorRecords errorRecords) {
		Random rand = new Random();
		Scanner scanner = new Scanner(System.in);
		int temp;
	    int asw;  
	    int userasw;
		int i = 1;
        while(i <= number) {
        	int fir = rand.nextInt(10) + 1; 
        	int sec = rand.nextInt(10) + 1;
        	int judge = rand.nextInt(10) + 1;
        	switch(judge%2) {
	        case 0:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	asw = fir + sec;
	        	System.out.println(fir + "+" + sec + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！"
	        						+ "正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, 0, 1, 0, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, 0, 1, 0, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        		
	        case 1:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	if (fir < sec) {
	        		temp = fir;
	        		fir = sec;
	        		sec = temp;
	        	}
	        	asw = fir - sec;
	        	System.out.println(fir + "-" + sec + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                // 清除输入缓冲区中的无效输入  
	                scanner.next(); // 这会读取并丢弃无效输入  
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, 0, 2, 0, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, 0, 2, 0, asw);
	        		}
	        	}
	        	i++;
	        		
	        default:
	        	break;
	        }
        }
        //scanner.close();
	}
	
	public static void ShiYiNeiErCiJiaJian (int number, int index, ErrorRecords errorRecords) {
		Random rand = new Random();
		Scanner scanner = new Scanner(System.in);
		int fir, sec, trd;
		int temp;
	    int asw;  
	    int userasw;
		int i = 1;
		int judge;
        while(i <= number) {
        	fir = rand.nextInt(10) + 1; 
        	sec = rand.nextInt(10) + 1;
        	trd = rand.nextInt(10) + 1;
        	judge = rand.nextInt(10) + 1;
        	switch(judge%4) {
	        case 0:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	asw = fir + sec + trd;
	        	System.out.println(fir + "+" + sec + "+" + trd + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, trd, 1, 1, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, trd, 1, 1, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        		
	        case 1:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	if (fir + sec < trd) {
	        		temp = fir;
	        		fir = trd;
	        		trd = temp;
	        	}
	        	asw = fir + sec - trd;
	        	System.out.println(fir + "+" + sec + "-" + trd + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, trd, 2, 1, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, trd, 2, 1, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        
	        case 2:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	while (fir < sec + trd) {
	        		fir = rand.nextInt(10) + 1; 
	            	sec = rand.nextInt(10) + 1;
	            	trd = rand.nextInt(10) + 1;
	        	}
	        	asw = fir - sec - trd;
	        	System.out.println(fir + "-" + sec + "-" + trd + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, trd, 4, 1, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, trd, 4, 1, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        
	        case 3:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	if (fir < sec) {
	        		temp = fir;
	        		fir = sec;
	        		sec = temp;
	        	}
	        	asw = (fir - sec) + trd;
	        	System.out.println(fir + "-" + sec + "+" + trd + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, trd, 3, 1, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, trd, 3, 1, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        		
	        default:
	        	break;
	        }
        }
        //scanner.close();
	}
	
	public static void ShiDaoErShiYiCiJiaJian (int number, int index, ErrorRecords errorRecords) {
		Random rand = new Random();
		Scanner scanner = new Scanner(System.in);
		System.out.print("请选择题量：");
		int temp;
	    int asw;  
	    int userasw;
		int i = 1;
		while (i <= number) {
	    	int fir = rand.nextInt(21 - 10) + 10; 
	    	int sec = rand.nextInt(21 - 10) + 10;
	    	int judge = rand.nextInt(21 - 10) + 10;
	    	switch(judge%2) {
	        case 0:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	asw = fir + sec;
	        	System.out.println(fir + "+" + sec + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, 0, 1, 0, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, 0, 1, 0, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        		
	        case 1:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	if (fir < sec) {
	        		temp = fir;
	        		fir = sec;
	        		sec = temp;
	        	}
	        	asw = fir - sec;
	        	System.out.println(fir + "-" + sec + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, 0, 2, 0, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, 0, 2, 0, asw);
	        		}
	        	}
	        	i++;
	        		
	        default:
	        	break;
	        }
		}
        //scanner.close();
	}
	
	public static void ShiDaoErShiErCiJiaJian (int number, int index, ErrorRecords errorRecords) {
		Random rand = new Random();
		Scanner scanner = new Scanner(System.in);
		System.out.print("请选择题量：");
		int fir, sec, trd;
		int temp;
	    int asw;  
	    int userasw;
		int i = 1;
		while (i <= number) {
			fir = rand.nextInt(21 - 10) + 10;
	    	sec = rand.nextInt(21 - 10) + 10;
	    	trd = rand.nextInt(21 - 10) + 10;
	    	int judge = rand.nextInt(10) + 1;
	    	switch(judge%4) {
	        case 0:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	asw = fir + sec + trd;
	        	System.out.println(fir + "+" + sec + "+" + trd + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, trd, 1, 1, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, trd, 1, 1, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        		
	        case 1:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	if (fir + sec < trd) {
	        		temp = fir;
	        		fir = trd;
	        		trd = temp;
	        	}
	        	asw = fir + sec - trd;
	        	System.out.println(fir + "+" + sec + "-" + trd + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, trd, 2, 1, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, trd, 2, 1, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        
	        case 2:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	while (fir < (sec + trd)) {
	        		fir = rand.nextInt(21 - 10) + 10; 
	            	sec = rand.nextInt(21 - 10) + 10;
	            	trd = rand.nextInt(21 - 10) + 10;
	        	}
	    
	        	asw = fir - sec - trd;
	        	System.out.println(fir + "-" + sec + "-" + trd + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, trd, 4, 1, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, trd, 4, 1, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        
	        case 3:
	        	if(number != 1) {
					System.out.println("第"+ i +"题");
				}
	        	if (fir < sec) {
	        		temp = fir;
	        		fir = sec;
	        		sec = temp;
	        	}
	        	asw = (fir - sec) + trd;
	        	System.out.println(fir + "-" + sec + "+" + trd + "=");
	        	while (!scanner.hasNextInt()) {  
	                System.out.println("输入无效，请重新输入一个整数: ");  
	                scanner.next();
	            } 
	        	userasw = scanner.nextInt();
	        	if (userasw == asw) {
	        		System.out.println("答案正确！");
	        	}
	        	else {
	        		System.out.println("答案错误！\n正确答案是：" + asw);
	        		if(index == 500) {
	        			modifyErrorRecorder(errorRecords, i, fir, sec, trd, 3, 1, asw);
	        		}
	        		else {
	        			modifyErrorRecorder(errorRecords, index, fir, sec, trd, 3, 1, asw);
	        		}
	        	}
	        	i++;
	        	break;
	        		
	        default:
	        	break;
	        }
		}
	    	
        //scanner.close();
	}
	
	public static void caculate_10_20 (int number, int index, ErrorRecords errorRecords) {
		Random rand = new Random();
		int num;
		int i = 1;
		while (i <= number) {
			num = rand.nextInt(10) + 1;
			switch (num%2) {
			case 0:
				if(number != 1) {
					System.out.println("第"+ i +"题");
				}
				ShiDaoErShiYiCiJiaJian(1, index, errorRecords);
				i++;
				break;
			
			case 1:
				if(number != 1) {
					System.out.println("第"+ i +"题");
				}
				ShiDaoErShiErCiJiaJian(1, index, errorRecords);
				i++;
				break;
			}
		}
	}
}